package com.example.exceltosqlite.ui.sqldao.insert;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.exceltosqlite.R;
import com.example.exceltosqlite.base.BaseActivity;
import com.example.exceltosqlite.bean.MyConstant;
import com.example.exceltosqlite.dao.ExcelFileDao;
import com.example.exceltosqlite.entity.SqliteFileEntity;
import com.example.exceltosqlite.entity.SqliteFileNumberEntity;

import org.greenrobot.eventbus.EventBus;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SqlInsertActivity extends BaseActivity {
    @BindView(R.id.typeTV)
    TextView typeTV;
    @BindView(R.id.numberTV)
    TextView numberTV;
    @BindView(R.id.insertLV)
    ListView insertLV;
    @BindView(R.id.addBT)
    Button addBT;
    @BindView(R.id.insertBT)
    Button insertBT;
    @BindView(R.id.text00ET)
    EditText text00ET;
    @BindView(R.id.text01ET)
    EditText text01ET;
    @BindView(R.id.text02ET)
    EditText text02ET;
    @BindView(R.id.text03ET)
    EditText text03ET;
    @BindView(R.id.text04ET)
    EditText text04ET;
    @BindView(R.id.text05ET)
    EditText text05ET;
    @BindView(R.id.text06ET)
    EditText text06ET;
    @BindView(R.id.text07ET)
    EditText text07ET;
    @BindView(R.id.text08ET)
    EditText text08ET;
    @BindView(R.id.text09ET)
    EditText text09ET;
    @BindView(R.id.nameTV)
    TextView nameTV;
    private String intentFileTypeName;
    private SqliteFileNumberEntity intentSqliteNumberEntity; //当前选中的   number表数据
    private List<SqliteFileEntity> thisItems = new ArrayList<>();
    private SqlInsertAdapter sqlInsertAdapter;
    private ExcelFileDao excelFileDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sql_insert);
        ButterKnife.bind(this);
        init();
    }


    private void init() {
        intentFileTypeName = getIntent().getStringExtra(MyConstant.STR_NAME_1);
        intentSqliteNumberEntity = (SqliteFileNumberEntity) getIntent().getSerializableExtra(MyConstant.STR_NAME_2);
        typeTV.setText(intentFileTypeName);
        numberTV.setText(intentSqliteNumberEntity.getMessageNo());
        nameTV.setText(intentSqliteNumberEntity.getMessageName());
        sqlInsertAdapter = new SqlInsertAdapter(this);
        insertLV.setAdapter(sqlInsertAdapter);
        excelFileDao = new ExcelFileDao(this);
        sqlInsertAdapter.setItems(thisItems);

    }


    @OnClick({R.id.addBT, R.id.insertBT})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.addBT:
                addInputDatas();
                break;
            case R.id.insertBT:
                if (0 == sqlInsertAdapter.getItems().size()) {
                    Toast.makeText(this, "请先添加或保存新增数据", Toast.LENGTH_SHORT).show();
                    return;
                }
                try {
                    excelFileDao.insertAll(sqlInsertAdapter.getItems());
                    EventBus.getDefault().post(MyConstant.EVENT_MSG_2);
                    Toast.makeText(this, "新增成功", Toast.LENGTH_SHORT).show();
                } catch (SQLException e) {
                    e.printStackTrace();
                    Toast.makeText(this, "新增失败" + e.getMessage(), Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    private void addInputDatas() {
        thisItems.add(new SqliteFileEntity(intentSqliteNumberEntity.getId(), text00ET.getText().toString().trim(), text01ET.getText().toString().trim(), text02ET.getText().toString().trim(), text03ET.getText().toString().trim(), text04ET.getText().toString().trim(), text05ET.getText().toString().trim(), text06ET.getText().toString().trim(), text07ET.getText().toString().trim(), text08ET.getText().toString().trim(), text09ET.getText().toString().trim()));
        sqlInsertAdapter.setItems(thisItems);
        cleanET();
    }


    private void cleanET() {
        text00ET.setText("");
        text01ET.setText("");
        text02ET.setText("");
        text03ET.setText("");
        text04ET.setText("");
        text05ET.setText("");
        text06ET.setText("");
        text07ET.setText("");
        text08ET.setText("");
        text09ET.setText("");
    }
}
